use std::fs::{File, OpenOptions, metadata, remove_file};
use std::io::{self, BufRead, BufReader, BufWriter, Read, Write};
use std::path::Path;
use crate::ltools::lstring;
use crate::lexpose;



/* 读取文件内容 */
pub(crate) fn read_file_line(path:&str, f:fn(String)) -> io::Result<()>{
    let open_file = File::open(path)?;
    let reader = BufReader::new(open_file);

    for line in reader.lines() {
        f(line?);
    }
    Ok(())
}

pub(crate) fn read_file_byte(path:&str, f:fn(String)) -> io::Result<()> {
    let open_file = File::open(path)?;
    let mut reader = BufReader::new(open_file);

    let mut byte_vec: Vec<u8> = Vec::new();
    let mut byte_vec1: Vec<u8> = Vec::new();
    let mut byte_vec2: Vec<u8> = Vec::new();
    let mut byte_vec3: Vec<u8> = Vec::new();
    let mut lines = 1;

    for res in reader.bytes() {
        match res {
            Ok(byte1) =>{
                if byte1 == b'\n' {
                    if byte_vec.len() > 0 {
                        let content = lstring::vec_string(&mut byte_vec);
                        // lexpose::print_info_log(content);
                        println!("lines: {}, content: {}", lines, content);
                        byte_vec.clear();
                    }
                    lines += 1;
                }else {
                    match lines {
                        (0..=31) =>{
                            byte_vec.push(byte1);
                        },
                        32 =>{
                            byte_vec1.push(byte1);
                        }
                        32 =>{
                            byte_vec2.push(byte1);
                        }
                        34 =>{
                            byte_vec3.push(byte1);
                        }
                        _ => {}
                    }
                }
            },
            Err(_) => {
                break;
            }
        }
    }

    println!("输出数据： {:?}", byte_vec1);
    println!("输出数据： {:?}", byte_vec2);
    println!("输出数据： {:?}", byte_vec3);

    Ok(())
}

pub(crate) fn write_file_byte(path: &str, content: &[u8], if_append: bool) -> io::Result<()> {
    let option_file = OpenOptions::new()
        .write(true)
        .append(if_append)
        .create(true)
        .open(path)?;

    let mut writer: BufWriter<File> = BufWriter::new(option_file);
    writer.write_all(content)?;
    writer.flush()?;
    return Ok(());
}

/**
 * 写入文件，如果文件不存在就新建一个文件
 * path 文件地址
 * content 写入内容
 * if_append 是否在后面追加
 */
pub(crate) fn write_file(path: &str, content: &str, if_append: bool) -> io::Result<()> {
    write_file_byte(path, content.as_bytes(), if_append)?;
    return Ok(());
}

/**
 * 读取地址中的全部文件
 */
pub(crate) fn read_all_file(path: &str) -> io::Result<Vec<(String, String)>> {
    return read_all_file_path(Path::new(path))
}

/**
 * 读取文件夹中的全部文件
 */
pub(crate) fn read_all_file_path
(path: &Path) -> io::Result<Vec<(String, String)>> {
    let mut file_list:Vec<(String, String)> = Vec::new();
    for entity in std::fs::read_dir(path)? {
        let entity = entity?;
        let path: std::path::PathBuf = entity.path();
        if entity.file_type()?.is_dir() {
            // read_all_file_path(path.as_path())?;
            continue;
        }
        let path_str = path.file_name().unwrap().to_str().unwrap();
        let path_parent = path.parent().unwrap().to_str().unwrap();

        file_list.push((format!("{}/{}", path_parent, path_str), path_str.to_string()));
    }
    return Ok(file_list);
}

/* 判断文件大小 */
pub(crate) fn file_size(path: &str) -> u64{
    match  metadata(path) {
        Ok(m ) => {
            m.len()
        }
        Err(_e) =>{
            0
        }
    }
}

/* 删除文件 */
pub(crate) fn delete(path:&str) {
    match remove_file(path) {
        Ok(m) => {}
        Err(_e) => {}
    }
}
















